package com.yunche.yunchebusiness.ui.activity.main

import android.annotation.SuppressLint
import android.widget.Toast
import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import com.bigkoo.pickerview.builder.OptionsPickerBuilder
import com.bigkoo.pickerview.listener.OnOptionsSelectListener
import com.yunche.yunchebusiness.R
import com.yunche.yunchebusiness.api.Api
import com.yunche.yunchebusiness.base.BaseActivity
import com.yunche.yunchebusiness.common.CommonDialog
import com.yunche.yunchebusiness.entity.BankListEntity
import com.yunche.yunchebusiness.isIdCard
import com.yunche.yunchebusiness.mvp.contract.AddTenantContract
import com.yunche.yunchebusiness.mvp.presenter.AddTenantPresenter
import com.yunche.yunchebusiness.network.RequestJson
import com.yunche.yunchebusiness.ui.activity.MainActivity
import com.yunche.yunchebusiness.ui.view.optionView.OptionItemClickListener
import com.yunche.yunchebusiness.utils.MyToast
import com.yunche.yunchebusiness.utils.ToastyUtils
import kotlinx.android.synthetic.main.activity_append_tenant_info.*

class AppendTenantInfoActivity : BaseActivity(), AddTenantContract.View {
    private var bankListEntity = listOf<BankListEntity>()

    private val mPresenter by lazy {
        AddTenantPresenter()
    }
    var bankId = 0
    override fun setLayout(): Int = R.layout.activity_append_tenant_info

    override fun initView() {
        mPresenter.attachView(this)
        ToastyUtils.showInfo("首次登陆需要完善信息")
    }

    override fun initData() {
        var para = JSONObject()
        mPresenter.getBankList(RequestJson.getRequestBody(para.toJSONString()))
    }

    override fun getBankListSuccess(bankListEntity: List<BankListEntity>) {
        this.bankListEntity = bankListEntity
    }

    override fun submitSuccess() {
        ToastyUtils.showInfo("提交成功")
        openActivity(MainActivity::class.java)
        finish()
    }

    override fun showLoading() {
        CommonDialog.showLoading(mContext!!, "加载中...")
    }

    override fun hideLoading() {
        CommonDialog.cancle()
    }

    override fun showError(msg: String) {
        ToastyUtils.showError(msg)
    }

    private fun showBankList() {
        var bankList = arrayListOf<String>()
        for (bankListEntity in bankListEntity) {
            bankList.add(bankListEntity.name)
        }

        val pvOptions = OptionsPickerBuilder(mContext,
            OnOptionsSelectListener { options1, _, _, _ ->
                ov_account_bank_list.setContent(bankListEntity[options1].name)
                bankId = bankListEntity[options1].id
            }).build<String>()
        pvOptions.setPicker(bankList)
        pvOptions.show()
    }

    override fun initEvent() {
        btn_submit.setOnClickListener {

            var contactName = ov_contact_name.getContent()
            var name = ov_tenant_name.getContent()
            var contactPosition = ov_contact_position.getContent()

            var account = ov_account_card_num.getContent()
            var accountName = ov_account_bank_name.getContent()

            var idcard = ov_account_idcard.getContent()

            if (contactName.isEmpty() || account.isEmpty() || accountName.isEmpty()) {
                MyToast.makeTextToast(mContext!!, "请填写完整", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

            if (!idcard.isIdCard()) {
                ToastyUtils.showWarning("请输入正确的身份证号")
                return@setOnClickListener
            }

            if (bankId == 0) {
                ToastyUtils.showWarning("请选择银行")
                return@setOnClickListener
            }

            var tenant = JSONObject()
            tenant["contactName"] = contactName
            tenant["name"] = name
            tenant["contactPosition"] = contactPosition

            var accountJson = JSONObject()
            accountJson["account"] = account
            accountJson["accountName"] = account
            accountJson["bank"] = bankId
            accountJson["accoutCardId"] = idcard

            var accountList = JSONArray()
            accountList.add(accountJson)

            var para = JSONObject()
            para["accountList"] = accountList
            para["tenant"] = tenant

            mPresenter.submit(RequestJson.getRequestBody(para.toJSONString()), Api.getTenantId())

        }

        ov_account_bank_list.setOnOptionItemClickListener(object : OptionItemClickListener {
            @SuppressLint("CheckResult")
            override fun onOptionItemClickListener(position: Int) {
                showBankList()
            }
        })
    }

    override fun onDestroy() {
        super.onDestroy()
        mPresenter.detachView()
    }

}
